﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class FrmaddCourseInfo : Form
    {
        private string o_id = null;
        public FrmaddCourseInfo(string id)
        {
            InitializeComponent();
            this.o_id = id;
            if (o_id != "")
            {
                CourseInfoData info = new CourseInfoData();
                info.Kcid = id;
                DataSet ds = CourseInfoOperation.getCourseInfo(info);
                this.txtKcid.Text = ds.Tables[0].Rows[0]["课程编号"].ToString();
                this.txtKcname.Text = ds.Tables[0].Rows[0]["课程名称"].ToString();
                this.txtPeriodexpriment.Text = ds.Tables[0].Rows[0]["实验学时"].ToString();
                this.txtPeriodteaching.Text = ds.Tables[0].Rows[0]["讲课学时"].ToString();
                this.txtCredit.Text = ds.Tables[0].Rows[0]["总学分"].ToString();
                this.combCoursetype.Text = ds.Tables[0].Rows[0]["课程类型"].ToString();
                this.txtKcid.Enabled = false;
                this.Text = "课程信息修改";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string kcid = this.txtKcid.Text;
            string kcname = this.txtKcname.Text;
            string periodexpriment = this.txtPeriodexpriment.Text;
            string periodteaching = this.txtPeriodteaching.Text;
            string credit = this.txtCredit.Text;
            string coursetype = (string)this.combCoursetype.SelectedItem;
            if (kcid == null || kcid.Trim().Equals(""))
            {//课程编号不允许为空
                MessageBox.Show("请输入课程编号！", "提示");
                this.txtKcid.Focus();
                return;
            }
            if (kcname == null || kcname.Trim().Equals(""))
            {//课程名陈不允许为空
                MessageBox.Show("请输入课程名称！", "提示");
                this.txtKcname.Focus();
                return;
            }
            CourseInfoData data = new CourseInfoData();
            data.Kcid = kcid;
            data.Kcname = kcname;
            data.Periodexpriment = periodexpriment;
            data.Periodteaching = periodteaching;
            data.Credit = credit;
            data.Coursetype = coursetype;
            try
            {
                if (o_id == "")
                {
                    if (CourseInfoOperation.insertCourseInfo(data))
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.txtKcid.Text = "";//清空文本框
                        this.txtKcname.Text = "";
                        this.txtPeriodexpriment.Text = "";
                        this.txtPeriodteaching.Text = "";
                        this.txtCredit.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "错误");
                    }
                }
                else
                {
                    if (CourseInfoOperation.updateCourseInfo(data))
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！", "错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败！", "错误");
            }
        }

        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmaddCourseInfo_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 课程类型 from 课程信息表 group by 课程类型";
            SqlCommand cm = new SqlCommand(str, cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combCoursetype.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }
    }
}